<template>
  <div class="task-list">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>我的任务列表</span>
        <el-button type="primary" @click="showAddTaskModal" style="float: right;">添加任务</el-button>
      </div>
      <el-table :data="tasks" style="width: 100%">
        <el-table-column prop="name" label="任务名称" width="180"></el-table-column>
        <el-table-column prop="completed" label="完成状态" width="100">
          <template slot-scope="scope">
            <el-tag :type="scope.row.completed ? 'success' : 'info'">
              {{ scope.row.completed ? '已完成' : '未完成' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="mini" @click="editTask(scope.row)">修改</el-button>

          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      tasks: []
    };
  },
  created() {
    this.fetchTasks();
  },
  methods: {
    fetchTasks() {
      const token = localStorage.getItem('token');
      axios.get('/api/tasks/mytasks', {
        headers: {
          'Authorization': token
        }
      })
          .then(response => {
            this.tasks = response.data;
          })
          .catch(error => {
            this.$message.error('获取任务列表失败');
          });
    },
    showAddTaskModal() {
      // 打开一个模态框或其他界面用于添加新任务
      this.$router.push('/addtodo'); // 假设你有一个路由用于添加任务
    },
    editTask(task) {
      // 导航到编辑任务页面，使用 query 参数传递 taskId
      console.log(task)
      this.$router.push({
        name: 'edit-task',
        query: { taskId: task.id }
      });
    }
  }
};
</script>
